//
//  SleepSevenDaysTrendsViewModel.swift
//  DaRings
//
//  Created by 尹琼 on 2022/11/4.
//

import UIKit

import RxCocoa
import RxSwift
import MYModule

open class SleepSevenDaysTrendsViewModel: ViewModel {
    
    
    public static let YMax: Double = 720
    public static let YMin: Double = 36.0
    
    public let providerRelay: BehaviorRelay<SleepProvider?>
    public let recentSevenDay = BehaviorRelay<[DFBarDataModel]>(value: [])
    public let recentSevenSleepTime = BehaviorRelay<[(hour: String, minute: String)]>(value: [])
    public let avgSleepTime = BehaviorRelay<(hour: String, minute: String)>(value: ("--", "--"))
    
    
    public init(providerRelay: BehaviorRelay<SleepProvider?>) {
        self.providerRelay = providerRelay
        super.init()
        
        providerRelay.map { provider ->[DFBarDataModel] in
            
            guard let records = provider?.sevenDaysRecord else {
                return []
            }
            
            let colors: [UIColor] = [
            
                UIColor.Sleep.restful,
                UIColor.Sleep.light,
                UIColor.Sleep.rem,
                
            ]
            
            var colorIndex = 0
            let list = records.map { record ->DFBarDataModel in
            
                let deep = Double(record.deep)
                let light = Double(record.light)
                let rem = Double(record.rem)
                
                // [deep, light, rem] [100, 200, 300]
                var model = DFBarDataModel(colors: colors, datas: [deep, light, rem])
                model.hightColor = UIColor.Sleep.theme
                colorIndex = colorIndex + 1
                if colorIndex >= colors.count {
                    colorIndex = 0
                }
                return model
            }
            
            return list
            
        }.bind(to: recentSevenDay).disposed(by: rx.disposeBag)
        
        
        providerRelay.map { provider ->[(hour: String, minute: String)] in
            
            guard let records = provider?.sevenDaysRecord else {
                return []
            }
            
            let timeList = records.map { record -> (hour: String, minute: String) in
                
                let hour = record.total/60
                let minute = record.total%60
                
                if hour == 0 && minute == 0 {
                    return ("--", "--")
                }
                return (hour.leadingZero, minute.leadingZero)
                
            }
            return timeList
            
        }.bind(to: recentSevenSleepTime).disposed(by: rx.disposeBag)
        
        
        providerRelay.map { provider ->(hour: String, minute: String) in
            
            guard let records = provider?.sevenDaysRecord else {
                return ("--", "--")
            }
            
            var allTotal = 0
            var count = 0
            for record in records {
                if record.total > 0 {
                    allTotal += record.total
                    count += 1
                }
                
            }
            if allTotal == 0 || records.isEmpty || count == 0 {
                return ("--", "--")
            }
            let avg = allTotal/count
            let hour = avg/60
            let minute = avg%60
            
            return (hour.leadingZero, minute.leadingZero)
            
        }.bind(to: avgSleepTime).disposed(by: rx.disposeBag)
        
        
    
    }

}
